package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.timepicker.MaterialTimePicker;
import com.google.android.material.timepicker.TimeFormat;

import java.util.Locale;

import cn.lrwjz.androidguide.R;

public class TimePickerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_picker);

        // 设置工具栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("TimePicker 示例");
        }

        // 12小时制时间选择器
        MaterialButton btn12HourPicker = findViewById(R.id.btn12HourPicker);
        btn12HourPicker.setOnClickListener(v -> {
            MaterialTimePicker picker = new MaterialTimePicker.Builder()
                    .setTimeFormat(TimeFormat.CLOCK_12H)
                    .setHour(9)
                    .setMinute(30)
                    .setTitleText("选择时间")
                    .build();

            picker.addOnPositiveButtonClickListener(dialog -> {
                int hour = picker.getHour();
                int minute = picker.getMinute();
                String period = hour < 12 ? "AM" : "PM";
                hour = hour % 12;
                hour = hour == 0 ? 12 : hour;
                String time = String.format(Locale.getDefault(), 
                    "%d:%02d %s", hour, minute, period);
                Toast.makeText(this, "选择的时间: " + time, Toast.LENGTH_SHORT).show();
            });

            picker.show(getSupportFragmentManager(), "TIME_PICKER_12H");
        });

        // 24小时制时间选择器
        MaterialButton btn24HourPicker = findViewById(R.id.btn24HourPicker);
        btn24HourPicker.setOnClickListener(v -> {
            MaterialTimePicker picker = new MaterialTimePicker.Builder()
                    .setTimeFormat(TimeFormat.CLOCK_24H)
                    .setHour(14)
                    .setMinute(30)
                    .setTitleText("选择时间")
                    .build();

            picker.addOnPositiveButtonClickListener(dialog -> {
                String time = String.format(Locale.getDefault(), 
                    "%02d:%02d", picker.getHour(), picker.getMinute());
                Toast.makeText(this, "选择的时间: " + time, Toast.LENGTH_SHORT).show();
            });

            picker.show(getSupportFragmentManager(), "TIME_PICKER_24H");
        });

        // 文本输入模式时间选择器
        MaterialButton btnTextInputPicker = findViewById(R.id.btnTextInputPicker);
        btnTextInputPicker.setOnClickListener(v -> {
            MaterialTimePicker picker = new MaterialTimePicker.Builder()
                    .setTimeFormat(TimeFormat.CLOCK_24H)
                    .setHour(14)
                    .setMinute(30)
                    .setTitleText("输入时间")
                    .setInputMode(MaterialTimePicker.INPUT_MODE_KEYBOARD)
                    .build();

            picker.addOnPositiveButtonClickListener(dialog -> {
                String time = String.format(Locale.getDefault(), 
                    "%02d:%02d", picker.getHour(), picker.getMinute());
                Toast.makeText(this, "输入的时间: " + time, Toast.LENGTH_SHORT).show();
            });

            picker.show(getSupportFragmentManager(), "TIME_PICKER_TEXT");
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 